<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" ref="formRef">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="板号">
          <a-input placeholder="请输入板号" v-decorator="[ 'version', validatorRules.version]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="格子类型">
          <j-dict-select-tag allowClear placeholder="请选择" :triggerChange="true" dictCode="cellType" v-decorator="[ 'type', validatorRules.type]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="锁号">
          <a-input placeholder="请输入板号" v-decorator="[ 'lockNum', validatorRules.lockNum]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import { postAction } from '@api/manage'
import DeviceList from '@api/DeviceManage/DeviceList'
import * as api from '@api/api'

export default {
  name: 'CellBatchAdd',
  data() {
    return {
      title: '新增格子',
      modalWidth: 800,
      visible: false,
      confirmLoading: false,
      validatorRules: {
        version: {
          rules: [{
            required: true, message: '请输入板号!',
          }],
        },
        type: {
          rules: [{
            required: true, message: '请选择格子类型!',
          }],
        },
        lockNum:{
          rules: [{
            validator: (rule, value, callback) =>{
              let version = this.form.getFieldValue('version')
              let cellType = this.form.getFieldValue('type')
              let lockNum = this.form.getFieldValue('lockNum')
              if (lockNum === '0') {
                callback('锁号不能为0')
              } else if (version && cellType && lockNum) {
                let deviceId = this.record.deviceId
                postAction(DeviceList.lockNumExistence, { version, cellType, lockNum, deviceId }).then(res => {
                  res.result ? callback('该锁号已存在') : callback()
                }).catch(err => {
                  callback(err.message || err)
                })
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }],
        },
      },
      confirmDirty: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      form: this.$form.createForm(this),
      url: 'business/machineCell/batchAdd',
      record: {}
    }
  },
  methods: {
    handleCancel() {
      this.close()
    },
    async showModal(record) {
      this.visible = true
      this.record = record
    },
    close() {
      this.record = {}
      this.visible = false
      this.form = this.$form.createForm(this)
    },
    handleOk() {
      if(!this.record.id){
        that.$message.warning("请选择机器再操作！")
      }
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let params = Object.assign({}, values, { id: that.record.deviceId, quantity: 1 })
          postAction(this.url, params).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.close()
              this.$emit('refresh')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>

